<?php 
include_once "function_xml_basic.php";
include_once "function_misc.php";
include_once "function_base_xml_class.php";

class CBridig extends base_xml_class 
{
	function __construct()
	{
		$this->root_path = "/MINI/NETWORK/INTERFACES";
		$this->node_name = "INTERFACE";
		$this->key_array = array("Name", "Ports", "Stp", "IpMigration", "Enabled");
	}
}

function GetXmlBridgeList()
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Type='bridge']";

	$ret_array = array();
	$results = GetAllNodeValue($query_string);
	$i = 0;
	foreach ($results as $result)
	{
		$ret_array[$i]['Name'] = $result->getAttribute('Name');
                $ret_array[$i]['Ports'] = $result->getAttribute('Ports');
                $ret_array[$i]['Stp'] = $result->getAttribute('Stp');
                $ret_array[$i]['IpMigration'] = $result->getAttribute('IpMigration');
                $ret_array[$i]['Enabled'] = $result->getAttribute('Enabled');
                $ret_array[$i]['DHCP'] = $result->getAttribute('DHCP');
		
		$query_string2 ="/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $ret_array[$i]['Name']. 
				"' and @Type='bridge']/ADDRESSES/ADDRESS";

		$ret_number = GetNumberOfNode($query_string2);
		if ($ret_number > 0){
			$ret_address = GetAllNodeValue($query_string2);
			$k = 0;
			foreach ($ret_address as $address)
			{
				if ($address->getAttribute('Enabled') == 1 && 
				    $address->getAttribute('Ip') != ""){
					$ret_array[$i]['Ip'][$k] = $address->getAttribute('Ip');		
					$ret_array[$i]['Mask'][$k] = $address->getAttribute('Mask');
					$k ++;
				}
			}
		}
		else{
			$ret_array[$i]['Ip'][0] = "";
			$ret_array[$i]['Mask'][0] = "";
		}

                $i ++;
	}
	return $ret_array;
}

#$ret_array = GetXmlBridgeList();
#foreach ($ret_array as $ret)
#{
#	print $ret['Name'];
#	foreach ($ret['Ip'] as $tmp)
#		print $tmp;
#}

function GetXmlBridgeListNumber()
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Type='bridge']";
	$ret_number = GetNumberOfNode($query_string);
	return $ret_number;
}
function GetXmlBridgeInfoByName($birdge_name)
{
	$ret_array = GetXmlBridgeList();
	
	foreach ($ret_array as $ret)
	{
		if ($ret['Name'] == $birdge_name)
			return $ret;
	}
	return NULL;
}

function DelXmlNetDevOneBridge($name)
{
	FlushSysAllBridgeConfig();
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $name. "']";
	DelSpecialNode($query_string);
	ApplySysAllBridgeDevInfo();
}

function DelXmlNetDevAllBridge()
{
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Type='bridge']";
	DelSpecialNode($query_string);
	FlushSysAllBridgeConfig();
}

function AppendXmlNetDevOneBridge($Valu_array)
{
	FlushSysAllBridgeConfig();
	$node_name = "INTERFACE";
	$query_string = "/MINI/NETWORK/INTERFACES";
	$Valu_array['Type'] = "bridge";
	
	$temp_array ['Ip'] = $Valu_array['Ip'];
	$temp_array ['Mask'] = $Valu_array['Mask'];
	$temp_array ['Enabled'] = "1";
	unset($Valu_array['Ip']);
	unset($Valu_array['Mask']);
	AppendAllAttrOfNode($query_string, $node_name, $Valu_array);

	$query_string .= "/". $node_name. "[last()]";
	$node_name = "ADDRESSES";
	$null_array = array ();
	AppendAllAttrOfNode($query_string, $node_name, $null_array);
	
	if ($temp_array['Ip'] != ''){
		$query_string .= "/". $node_name;
		$node_name = "ADDRESS";
		AppendAllAttrOfNode($query_string, $node_name, $temp_array);
	}
	
	ApplySysAllBridgeDevInfo();
}

function EditXmlNetDevBridge($in_array)
{
	FlushSysAllBridgeConfig();
	$dev = $in_array['Name'];
	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']";
	$edit_key['IpMigration'] = $in_array['IpMigration'];
	$edit_key['Stp'] = $in_array['Stp'];
	$edit_key['Ports'] = $in_array['Ports'];
	$edit_key['Enabled'] = $in_array['Enabled'];
	$edit_key['DHCP'] = $in_array['DHCP'];
	EditAllAttrOfNode($query_string, $edit_key);

	$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']/ADDRESSES/ADDRESS";
	if ($in_array['Ip'] == ""){
		DelSpecialNode($query_string. '[1]');
	}
	else {
		$ex = GetNumberOfNode($query_string);
		$edit_key2['Ip'] = $in_array['Ip'];
		$edit_key2['Mask'] = $in_array['Mask'];
		$edit_key2['Enabled'] = 1;

		if ($ex == 0){
			$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE[@Name='". $dev. "']/ADDRESSES";
			$node_name = "ADDRESS";
			AppendAllAttrOfNode($query_string, $node_name, $edit_key2);
		}
		else {
			EditAllAttrOfNode($query_string, $edit_key2);
		}
	}	
	
	ApplySysAllBridgeDevInfo();
}

function GetSpecialBirdgeDevList($list_string)
{
	$ret_array = explode(",", $list_string);
	if ($ret_array[count($ret_array) - 1] == '')
		array_pop($ret_array);
	#foreach ($ret_array as $i)
	#{
	#	print "[". $i. "]\n"; 
	#}
	return $ret_array;
}

function GetBridgeDevListFromSys()
{
	$command_string = "/usr/local/sbin/brctl show | grep -E 'no|yes' | awk '{print $1}'";
	$ret_string = shell_exec($command_string);
	
	$ret_array = explode("\n", $ret_string);
	
	#foreach ($ret_array as $ret)
	#{
	#	print "[". $ret. "]\n";
	#}
	return $ret_array;
}

function GetBridgeDevInfoFromSys($br)
{
	$command_string = "/usr/local/sbin/brctl showstp ". $br. " | grep eth[0-9] | awk '{print $1}'";
	$ret_string = shell_exec($command_string);
	
	$ret_array = explode("\n", $ret_string);
	
	#foreach ($ret_array as $ret)
	#{
	#	print "[". $ret. "]\n";
	#}
	return $ret_array;
}

include_once "function_misc.php";
include_once "function_net_tools.php";

function ApplySysAllBridgeDevInfo()
{
	$command = "";
	$command_brctl = "/usr/local/sbin/brctl ";
	$command_ifconfig = "/sbin/ifconfig ";
	$command_dhclient = "/sbin/dhclient ";
	
	$ret_br_list = GetXmlBridgeList();	
	foreach($ret_br_list as $br)
	{
		if ($br['Enabled'] == 0)
			continue;

		$addr_list = array();
		$i = 1;

		$ports_string = DelEndComma($br['Ports']);
		
		$eth_list = explode(",", $ports_string);
		foreach ($eth_list as $eth)
		{
			if ($eth == "")
				continue;
			$command .= $command_ifconfig. " ". $eth. " 0 ;";
			$eth_info = GetXmlNetDevInfo($eth);
			$eth_address_list = $eth_info['Address'];
			$addr_list[$i++] = $eth_address_list;
		}

		$ports = str_replace(","," ",$ports_string);
		
		$stp = "";
		if ($br['Stp'] == 1)
			$stp = " on ";
		else
			$stp = " off ";
		
		$command .= $command_brctl. " addbr ". $br['Name']. " ;";
		$command .= $command_brctl. " addif ". $br['Name']. " ". $ports. " ;";
		$command .= $command_brctl. " stp ". $br['Name']. $stp. " ;";
		$command .= $command_ifconfig. $br['Name']. " up ; ";
		
		$command_dhcp_kill = "/bin/kill -9  `/bin/ps | /bin/grep \"dhclient ". 
				     $br['Name']. "\" | /bin/grep  \" S \" | /bin/awk '{printf $1 \" \" }'` >/dev/null 2>&1";
		$ret_command = shell_exec($command_dhcp_kill);
		if ($br['DHCP'] == 1){
			$command .= $command_dhclient . " ". $br['Name']. " ;";
		}
		else if ($br['DHCP'] == 0){
			$n = 0;
			$command_ip = "";
			$command_end = "";
			//print_r ($br['Ip']);
			foreach ($br['Ip'] as $ip)
			{
				if ($n == 0){
					if ($ip != ""){
						$command_ip .= $command_ifconfig. $br['Name']. " ". $ip. " ";
						$command_end = ";";
					}	
					if ($br['Mask'][$n] != "")
						$command_ip .= " netmask ". $br['Mask'][$n]. " ";
				}	
				else{
					if ($ip != ""){
						$command_ip .= $command_ifconfig. $br['Name']. ":". $n. " ". $ip. " ";
						$command_end = ";";
					}	
					if ($br['Mask'][$n] != "")
						$command_ip .= " netmask ". $br['Mask'][$n]. " ";

				}	
				$command_ip .= $command_end;

				$n ++;
			}
			$command .= $command_ip;
		}

		if ($br['IpMigration'] == 1){
			$i = $n;
			$command_m = "";
			//print_r ($addr_list);
			foreach ($addr_list as $r){
				foreach ($r as $l){
					if ($l['Enabled'] == 1)
						$command_m .= $command_ifconfig. $br['Name'].":". $i++ . " ". $l['Ip']. " netmask ". $l['Mask']. " ; ";
				}
			}	
		$command .= $command_m;
		$command_m = "";
		}
	}
	//print $command;
	$ret_string = shell_exec($command);
	return $ret_string;
}

function FlushSysAllBridgeConfig()
{
	$ret_array_br_list = GetBridgeDevListFromSys();

	$command = "";
	$command_brctl = "/usr/local/sbin/brctl ";
	$command_ifconfig = "/sbin/ifconfig ";

	foreach ($ret_array_br_list as $br)
	{
		if ($br == "")
			continue;

		$ret_br_info = GetBridgeDevInfoFromSys($br);	
		$ports_string = "";
		foreach ($ret_br_info as $port)
		{
			$ports_string .= " ". $port. " ";	
		}
		$command .= $command_brctl. " delif ". $br. " ". $ports_string. ";";
		$command .= $command_ifconfig. $br. " 0 ;";
		$command .= $command_ifconfig. $br. " down ;";
		$command .= $command_brctl. " delbr ". $br. ";";
	}
	//print $command;
	$ret_string = shell_exec($command);
	return $ret_string;
}

?>
